import Vue from 'vue'
import Router from 'vue-router'

const Home = () => import('views/home/home.vue')
const Login = () => import("views/login/index.vue")
const Delicious = () => import("views/delicious/delicious.vue")
const Restaurant = () => import("views/restaurant/restaurant.vue")
Vue.use(Router)

const routes = [
  {
    path: '/BScroll',
    name: 'BScroll',
    component: () => import("views/BetterScroll.vue")
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/delicious',
    name: 'delicious',
    component: Delicious
  },
  {
    path: '/order',
    name: 'order',
    component: () => import("views/order/order.vue")
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import("views/mine/mine.vue")
  },
  {
    path: '/restaurant',
    component: Restaurant,
    children: [
      {
        path: '',
        redirect: 'commodity'
      },
      {
        path: 'commodity',
        component: () => import("views/restaurant/commodity/commodity.vue"),
        name: 'commodity',
      },
      {
        path: 'comment',
        component: () => import("views/restaurant/comment/comment.vue"),
        name: 'comment',
      },
      {
        path: 'seller',
        component: () => import("views/restaurant/seller/seller.vue"),
        name: 'seller',
      },
    ]

  }, 
  // {
  //   path: '/food',
  //   name: 'food',
  //   component: () => import("components/cartcontrol/shoppingcart.vue"),
  // }

]
const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to.name);
  document.title = to.name
  // console.log(localStorage.getItem('ele_login'));
  const isLogin = localStorage.getItem('ele_login') ? true : false;
  if (to.path == "/login") {
    next()
  } else {
    isLogin ? next() : next('/login');
    // next()
  }
})


// 避免路由重复跳转
// 获取原型对象上的push函数
const VueRouterPush = Router.prototype.push
// 修改原型对象中的push方法
Router.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}


export default router